﻿var secs = 60;
function updateTime() {
    secs--;
    if (secs <= 1) {
        $("#btn_sendmobilecode").attr("href", "javascript:sendmobilemsg();");
        $("#btn_sendmobilecode").html("重发验证码");
        $("#btn_sendmobilecode").removeClass("cGray93");
        $("#btn_sendmobilecode").addClass("beforesend");
    } else {
        $("#btn_sendmobilecode").attr("href", "javascript:void(0)");
        $("#btn_sendmobilecode").html("重发验证码(" + secs + "秒)");
        $("#btn_sendmobilecode").removeClass("beforesend");
        $("#btn_sendmobilecode").addClass("cGray93");
    }
}
function refreshC_code() {
    var pp = Math.round((Math.random()) * 100000000);
    document.getElementById("c_codeImg").src = "/vcodepic.aspx?userid=" + pp;
}
function checktime() {
    $("#btn_sendmobilecode").html("重发验证码(" + secs + "秒)");
    $("#btn_sendmobilecode").removeClass("beforesend");
    $("#btn_sendmobilecode").addClass("cGray93");
    window.setInterval("updateTime()", 1000);
}
function sendmobilemsg() {
    var errorDiv = $("#strError");
    var errorTxt = $("#strError").find("label");
    errorDiv.hide();
    errorTxt.html("");
    $.ajax({
        type: "GET",
        url: "https://reg.hexun.com/h5/h5_ajax.aspx",
        dataType: "jsonp",
        data: {
            id: $("#userId").val(),
            mobile: $("#mobile").val(),
            code: $("#code").val(),
            act: "sendactivemobilecode"
        },
        success: function (result) {
            if (result["state"] == "Y") {
                secs = 60;
            } else {
                errorDiv.show();
                errorTxt.html(result["msg"]);
            }
        },
        error: function (result) {
            alert("出现未知错误！");
        },
        xhrFields: {
            withCredentials: true
        }
    });
}

function fucCheckLength(strTemp) {
    var i, sum;
    sum = 0;
    for (i = 0; i < strTemp.length; i++) {
        if ((strTemp.charCodeAt(i) >= 0) && (strTemp.charCodeAt(i) <= 255))
            sum = sum + 1;
        else
            sum = sum + 2;
    }
    return sum;
}
function isMobile(str) {
    var re = /^0?1[3|5|7|8][0-9]\d{8}$/;
    if (re.test(str))
        return true;
    return false;
}

function isEmail(str) {
    if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(str))
        return false;
    return true;
}

function CheckUserName(obj) {
    var username = $("#txtUsername").val().replace(/(^\s+)|(\s+$)/g, "").replace(/\s/g, "");
    $("#txtUsername").val(username);
    if (username == "" || username == "邮箱/手机号") {
        if (obj == 0) {
            alert("请输入邮箱!");
            return false;
        } else if (obj == 1) {
            alert("请输入手机号!");
            return false;
        }
        alert("请输入邮箱或手机号");
        return false;
    }
    if (obj == 0 && !isEmail(username)) {
        alert("请输入正确的邮箱!");
        return false;
    } else if (obj == 1 &&!isMobile(username)) {
        alert("手机号有误或不支持!");
        return false;
    }
    return true;
}

function checkMobileCode() {
    $.ajax({
        type: "GET",
        url: "https://reg.hexun.com/h5/h5_ajax.aspx",
        dataType: "jsonp",
        data: {
            id: escape($("#userId").val()),
            mobile: escape($("#mobile").val()),
            yzm: escape($("#txt_yzm").val()),
            code: escape($("#code").val()),
            gourl: escape($("#gourl").val()),
            act: "checkmobilecode"
        },
        success: function (result) {
            if (result["state"] == "Y") {
                location.href = "https://reg.hexun.com/h5/h5_Success.aspx?gourl=" + escape($("#gourl").val()) + "&client=" + escape($("#client").val()) + "&top=" + escape($("#showTop").val());
                return false;
            } else {
                alert(result["msg"]);
            }
            return true;
        },
        error: function (result) {
            alert("出现未知错误！");
        },
        xhrFields: {
            withCredentials: true
        }
    });
    return true;
}
function valPwd(v) {
    var r = /^[a-zA-Z0-9]+$/g;
    return r.test(v);
}

function CheckPassword() {
    var reg = /^(([a-z]+[0-9]+)|([0-9]+[a-z]+))[a-z0-9]*$/i;
    var spReg = /^[A-Za-z0-9]+$/;
    var password = $("#txtPassword").val();
    $("#txtPassword").val(password);
    $("#pswming").val(password);
    if (!valPwd(password)) {
        alert("请输入6至20位的数字和英文组合密码");
        return false;
    }
    if (password == "" || password == "设置密码") {
        alert("请输入6至20位的数字和英文组合密码");
        return false;
    }
    if (password.length > 20) {
        alert("密码太长了，请输入6至20位的数字和英文组合密码");
        return false;
    }
    if (password.length < 6) {
        alert("密码太短了，请输入6至20位的数字和英文组合密码");
        return false;
    }
    if (spReg.test(password)) {
        if (!reg.test(password)) {
            alert("密码太简单了，请输入6至20位的数字和英文组合密码");
            return false;
        }
    }
    return true;
}

function CheckVcode() {
    var vcode = $("#txtVcode").val();
    if (vcode == "" || vcode == "验证码") {
        alert("请输入4位验证码");
        return false;
    }
    return true;
}

function checkEmail(email) {
    $.ajax({
        type: "GET",
        url: "https://reg.hexun.com/h5/h5_ajax.aspx",
        dataType: "jsonp",
        data: {
            email: escape(email),
            act: "checkemail"
        },
        success: function (result) {
            if (result["state"] != "Y") {
                alert(result["msg"]);
                return false;
            }
            return true;
        },
        error: function (result) {
            alert("出现未知错误！");
        },
        xhrFields: {
            withCredentials: true
        }
    });
    return true;
}
function checkMobile(mobile) {
    var errorDiv = $("#iptname_error");
    var errorTxt = $("#iptname_error").find("label");
    $.ajax({
        type: "GET",
        url: "https://reg.hexun.com/h5/h5_ajax.aspx",
        dataType: "jsonp",
        data: {
            mobile: escape(mobile),
            act: "checkmobile"
        },
        success: function (result) {
            if (result["state"] != "Y") {
                errorDiv.show();
                errorTxt.html(result["msg"]);
                return false;
            } else {
                errorDiv.hide();
                errorTxt.html("");
            }
            return true;
        },
        error: function (result) {
            alert("出现未知错误！");
        },
        xhrFields: {
            withCredentials: true
        }
    });
    return true;
}
function submitform(obj) {
    if (CheckUserName(obj) && CheckPassword() && CheckVcode()) {
        var client = $("#client").val();
        $.ajax({
            type: "GET",
            url: "https://reg.hexun.com/h5/h5_ajax.aspx",
            dataType: "jsonp",
            data: {
                name: escape($("#txtUsername").val()),
                password: escape($("#txtPassword").val()),
                gourl: escape($("#gourl").val()),
                referUrl: escape($("#referUrl").val()),
                fromhost: escape($("#fromhost").val()),
                client: escape(client),
                vcode: escape($("#txtVcode").val()),
                act: "reguser"
            },
            success: function (result) {
                if (result["code"] == "Y") {
                    if (obj == 1) {
                        location.href = "/h5/verify_tel.aspx?id=" + result["userid"] + "&mobile=" + escape($("#txtUsername").val()) + "&code=" + result["secrit"] + "&gourl=" + escape($("#gourl").val()) + "&fromhost=" + escape($("#fromhost").val()) + "&client=" + client + "&top=" + escape($("#showTop").val());
                    } else {
                        location.href = "/h5/verify_email.aspx?id=" + result["userid"] + "&email=" + escape($("#txtUsername").val()) + "&code=" + result["secrit"] + "&gourl=" + escape($("#gourl").val()) + "&fromhost=" + escape($("#fromhost").val()) + "&client=" + client + "&top=" + escape($("#showTop").val());
                    }
                } else {
                    var json = eval(result.options);
                    var txt = "注册失败，原因：\n";
                    for (var j = 0; j < json.length; j++) {
                        if (json[j]["state"] == "N") {
                            txt += "·" + json[j]["msg"] + "\n";
                        }
                    }
                    alert(txt);
                }
            },
            error: function (result) {
                alert("出现未知错误！");
            },
            xhrFields: {
                withCredentials: true
            }
        });
    }
}


$(document).ready(function () {
    $("#btn_regemail").click(function () {
        submitform(0);
    });
    $("#btn_regtel").click(function () {
        submitform(1);
    });
    $("#btn_vactivemobile").click(function () {
        checkMobileCode();
    });
});